﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class frm_Add_ExtendIQ : Form
    {
        private INISettings mySettings;
        private SqlConnection myConnection;

        public List<string> rzcode_list;

        public frm_Add_ExtendIQ()
        {
            InitializeComponent();
            this.mySettings = new INISettings();
            this.myConnection = new SqlConnection(mySettings.SQLConnectionString);
            this.rzcode_list = new List<string>();
        }

        public bool CreateForm()
        {
            bool ret = true;
            if (this.rzcode_list.Count > 0)
            {
                for (int i = 0; i < this.rzcode_list.Count; i++)
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = this.rzcode_list[i];
                    item.Text = this.rzcode_list[i];
                    this.rzCode.Properties.Items.Add(item);
                }
            }

            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT IQID FROM [222_IQ] ORDER BY IQID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["IQID"].ToString();
                    item.Value = dataReader["IQID"].ToString();
                    this.iqID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                ret = false;
            }
            return ret;
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;
            string rz_code = null;
            string iq_id = null;
            float qty = 0;

            if (string.IsNullOrEmpty(this.rzCode.Text.Trim()))
            {
                err = "Please enter RZCode";
            }
            else
            {
                rz_code = this.rzCode.Text.Trim();
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.iqID.Text.Trim()))
                {
                    err = "Please enter IQID";
                }
                else
                {
                    iq_id = this.iqID.Text.Trim();
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.quantityValue.Text.Trim()))
                {
                    err = "Please enter Quantity";
                }
                else
                {
                    if (float.TryParse(this.quantityValue.Text.Trim(), out qty))
                    {
                        if (qty <= 0)
                        {
                            err = "Quantity must be greater than 0";
                        }
                    }
                    else
                    {
                        err = "Invalid Quantity";
                    }
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [241_ExportCheckEx] (RZCode, IQID, Quantity)"
                        + " VALUES ('" + rz_code + "', '" + iq_id + "', " + qty.ToString() + ")";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Extend IQ has been added Success!");
                    //this.Dispose();
                    clearForm();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Extend IQ. Please try again.");
                }
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
